/**
 * @author Leong Wai Lun A0032831R
 */

package properties;

import java.io.*;
import java.util.*;

import geoarea.GeoAreaInfoDTO;
import jobadvertisement.JobAdvertisementDTO;
import jobresponse.JobResponseDTO;
import jobseeker.JobSeekerAccountDTO;
import company.CompanyDTO;

/**
 * 
 *how to read data from file and write data to file
 *
 */
public class PropertiesDAO {
	protected Hashtable<String, PropertiesDTO> dtos = new Hashtable<String, PropertiesDTO>();
	
	public enum DTOTYPES {
		COMPANY("company"),
		JOBADVERTISEMENT("jobadvertisement"),
		GEOAREAINFO("geoareainfo"),
		JOBRESPONSE("jobresponse"),
		JOBSEEKERACCOUNT ("jobseekeraccount");
	    private final String directory;
	    DTOTYPES(String directory) {
	        this.directory = directory;
	    }
	}
	/**
	 * get the size of  hashtable
	 * @return hashtable size
	 */
	public int getTotal() {
		return dtos.size();
	}
	
	/**
	 * add propertiesDTO to hashtable
	 * @param PropertiesDTO
	 */
	public void addDTO(PropertiesDTO currentDTO) {
		dtos.put(currentDTO.id, currentDTO);
	}
	/**
	 * remove propertiesDTO from hashtable
	 * @param id of data
	 */
	public void removeDTO(String id) {
		dtos.remove(id);
	}
	/**
	 * get propertiesDTO from hashtable
	 * @param id of DTO
	 * @return propertiesDTO
	 */
	public PropertiesDTO getDTO(String id) {
		return dtos.get(id);
	}
	/**
	 * get keys from hashtable
	 * @return keys
	 */       
	public Enumeration<String> getIDs() {
		return dtos.keys();
	}
	/**
	 * 
	 * @return hashtable
	 */  
	public Hashtable<String, PropertiesDTO> getDTOHashTable() {
		return this.dtos;
	}
	/**
	 * Read data from files
	 * @param DTOTYPES
	 * @return boolean
	 */  
	protected boolean readDTOs(DTOTYPES dtoType) {	
		dtos.clear();
		
		File folder = new File("data/" + dtoType.directory);
		
		if (!folder.exists())
			return false;
		
                File[] listOfFiles = folder.listFiles();

		for (int i = 0; i < listOfFiles.length; i++) {
			if (listOfFiles[i].isFile()) {
				String filename = listOfFiles[i].getName();
				Properties currentProperties = new Properties();
				try {
					//currentProperties.load(new FileInputStream("data/" + dtoType.directory + "/" + filename));
					// edit by Zhang Na. 
					//The file stream needs to be closed after use otherwise we can't delete the file later on.
					FileInputStream s = new FileInputStream("data/" + dtoType.directory + "/" + filename);
					currentProperties.load(s);
					s.close();
				} catch (IOException e) {
					return false;
				}
				
				PropertiesDTO currentPropertiesDTO = dtoMiniFactory(dtoType);
				currentPropertiesDTO.setProperties(currentProperties);
				
                                try {
                                    if (currentPropertiesDTO.id != null) {
                                        dtos.put(currentPropertiesDTO.id, currentPropertiesDTO);
                                    }
                                    
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
				
			}
		}
		
		return true;
	}
	/**
	 * Factory for new PropertiesDTO
	 * @param dtoType
	 * @return PropertiesDTO object
	 */
	private PropertiesDTO dtoMiniFactory(DTOTYPES dtoType) {
		switch(dtoType) {
		case COMPANY:
			return new CompanyDTO();
		
		case JOBADVERTISEMENT:
			return new JobAdvertisementDTO();
		
		case GEOAREAINFO:
			return new GeoAreaInfoDTO();
			
		case JOBRESPONSE:
			return new JobResponseDTO();
			
		case JOBSEEKERACCOUNT:
			return new JobSeekerAccountDTO();
		}
		
		return null;
	}
	/**
	 * write data to file
	 * @param dtoType
	 * @return boolean
	 */
	protected boolean writeDTOs(DTOTYPES dtoType) {
		String directory = "data/" + dtoType.directory;
		new File(directory).mkdirs();
		
		Enumeration<String> k = dtos.keys();

	    while (k.hasMoreElements()) {
	    	String currentKey = (String) k.nextElement();
	    	PropertiesDTO currentDTO = dtos.get(currentKey);
	    	Properties currentProperties = currentDTO.getProperties();
	    	
	    	try {
	    		// edit by Zhang Na. 
	    		//The file stream needs to be closed after use otherwise we can't delete the file later on.
	    		FileOutputStream s = new FileOutputStream(directory + "/" + currentKey);
	    		currentProperties.store(s, null);
	    		s.close();
	    		
	    		//currentProperties.store(new FileOutputStream(directory + "/" + currentKey), null);
	    	}
	    	catch (IOException e) {
	    		return false;
	    	}
	    }
	    
	    return true;
	}
}
